﻿using Ivony.Html;
using Ivony.Html.Parser;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Utility;

namespace YouWei.Console
{
    public class BbsScraping
    {
        private string TextSite = "http://bbs.cheshi.com/";

        public void Excute()
        {
            var list = GetCommentList("");

            var dto = GetCircleToComent(list.First());
            var entity = new GroupComment()
            {
                IsActvie = 1,
                CreateDate = DateTime.Now,
                IsHot = 0,
                CommentDesc = dto.CommentName,
                CommentName = dto.CommentName
            };
            //评论和浏览量随机人数
            entity.PageViews = entity.PageViews < 50 ? new Random().Next(50, 300) : entity.PageViews;
            entity.PraiseCount = entity.PraiseCount < 50 ? new Random().Next(50, 300) : entity.PraiseCount;

            var groupCommentID = Convert.ToInt32(new GroupCommentService().Insert(entity));
            var detailEntity = new GroupCommentDetail()
            {
                CreateDate = DateTime.Now,
                Detail = dto.Detail,
                GroupCommentID = groupCommentID
            };

        }

        /// <summary>
        /// 返回模块的文章链接列表
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public List<string> GetCommentList(string content)
        {
            var list = new List<string>();
            //var TextListUrl = "/forum-news-1449-1.html";
            ////IHtmlDocument source = new JumonyParser().LoadDocument(TextListUrl, System.Text.Encoding.GetEncoding("utf-8"));
            //RestClient client = new RestClient(TextSite);

            ////var request = new RestRequest("/2/short_url/shorten.json?source=2849184197&url_long=" + HttpUtility.UrlEncode(linkurl), Method.GET);
            //var request = new RestRequest(TextListUrl, Method.GET);
            //IRestResponse response = client.Execute(request);
            //var content = response.Content;
            IHtmlDocument source = new JumonyParser().Parse(content);
            //圈子内容
            var sqContent = source.Find(".sq_content");
            if (sqContent == null || sqContent.Count() < 1)
                return list;
            //圈子内容里的文章列表
            var sq_circleList = sqContent.Find(".sq_circleList");
            if (sq_circleList == null || sq_circleList.Count() < 1)
                return list;
            var aDoms = sq_circleList.Find("h3 a");
            if (aDoms == null || aDoms.Count() < 1)
                return list;

            foreach (var a in aDoms)
            {
                var href = a.Attribute("href");
                var strHref = href.AttributeValue;
                if (!string.IsNullOrWhiteSpace(strHref))
                    list.Add(strHref);
            }
            return list;
        }


        /// <summary>
        /// 解析文章详情
        /// </summary>
        /// <param name="href"></param>
        /// <returns></returns>
        public GroupCommentDto GetCircleToComent(string href)
        {
            var entity = new GroupCommentDto() { CreateDate = DateTime.Now };
            var TextListUrl = href;
            RestClient client = new RestClient(TextSite);
            var request = new RestRequest(TextListUrl, Method.GET);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            IHtmlDocument source = new JumonyParser().Parse(content);
            var sq_des = source.Find(".sq_des");
            if (sq_des == null || sq_des.Count() < 1)
                return entity;
            var title = sq_des.Find(".title a");
            if (title == null || title.Count() < 1)
                return entity;
            entity.CommentName = title.First().InnerText();
            var info = sq_des.Find(".sq_des_info");
            if (info == null || info.Count() < 1)
                return entity;

            var process = new CommentDetailProcess();
            entity.Detail = process.ProcessDetail(info.First().InnerHtml());
            return entity;
        }
    }
}
